import * as THREE from "three";
import Base3d from "./Base3d";

/**
 * @use 将页面的视角挪动到合适的位置 在控制台输入 generateCode() 浏览器控制台输出的代码拷贝使用即可
 */
export default class flyTo extends Base3d{
  init () {
    super.init()
    window.register("ON_CLICK_MAP",function (x,y){
      window.generateCode()
    })
    this.time = null;
    this.moveInfo = {
      points: [],
      lookAt: new THREE.Vector3(0,0,0)
    }
    let self = this;
    window.generateCode = function (){
      self.generateCode();
    }
    window.flyTo = function (descPosition,lookAt){
      self.flyTo(descPosition,lookAt)
    }
    window.flyToByDown = function (lngLat,height){
      self.flyToByDown(lngLat,height)
    }
    /**
     * 视角立马移动到指定的位置无动效
     * @param descPosition
     * @param lookAt
     */
    window.goTo = (descPosition,lookAt)=>{
      this.goTo(descPosition,lookAt)
    }
  }

  getMiddlePoint (point1,point2,percent = 0.5){
    return new THREE.Vector3((point2.x - point1.x) * percent + point1.x,(point2.y - point1.y) * percent + point1.y,(point2.z - point1.z) * percent + point1.z)
  }

  flyTo (cameraPosition,lookAt){
    cameraPosition = new THREE.Vector3(cameraPosition.x,cameraPosition.y,cameraPosition.z)
    let start = window.camera.position;
    let descPosition = cameraPosition;
    this.descPosition = cameraPosition;
    let cd = this.getMiddlePoint(start,new THREE.Vector3(descPosition.x / 2.0,descPosition.y / 2.0,descPosition.z / 2.0),0.7)
    let next = this.getMiddlePoint(start,new THREE.Vector3(descPosition.x ,descPosition.y,descPosition.z),0.9)
    this.controlPoint = descPosition;
    const curve = new THREE.CubicBezierCurve3(start,cd,next,descPosition);
    let points = curve.getPoints(500);
    points.unshift(new THREE.Vector3().subVectors(start,cd));
    this.moveInfo.points = points;
    this.moveInfo.lookAt = new THREE.Vector3(lookAt.x,lookAt.y,lookAt.z)
    this.time = 0;
  }
  goTo(cameraPosition,lookAt){
    cameraPosition = new THREE.Vector3(cameraPosition.x,cameraPosition.y,cameraPosition.z)
    this.controls.target.copy(lookAt);
    this.camera.position.copy(cameraPosition);
    this.controls.update();
    this.time = null;
  }

  /**
   * 以俯视的视角查看
   * @param location 坐标
   * @param height 高度
   */
  flyToByDown (location,height){
    let position = window.xbThreeFrame.getXy(location)
    this.flyTo({ x: position.x,y: height,z: position.y },{ x: position.x,y: 0,z: position.y })
  }

  generateCode (){
    let position = window.camera.position;
    //let rotate = window.xbThreeFrame.camera.rotation;
    let lookAt = this.controls.target;
    console.log(`window.flyTo({x:${position.x},y:${position.y},z:${position.z}},{x:${lookAt.x},y:${lookAt.y},z:${lookAt.z}})`)
    console.log(`window.goTo({x:${position.x},y:${position.y},z:${position.z}},{x:${lookAt.x},y:${lookAt.y},z:${lookAt.z}})`)
  }

  movingCamera () {
    if (this.time != null) {
      let lookAt = this.moveInfo.lookAt;
      this.time += 6;
      if (this.time < 501) {
        this.camera.position.copy(this.moveInfo.points[this.time]);
        this.camera.lookAt(lookAt.x, lookAt.y, lookAt.z)
        this.controls.saveState()
      } else if (this.time >= 501) {
        this.controls.target.copy(lookAt);
        this.controls.update();
        this.time = null;
      }
    }
  }

  render () {
    super.render()
    this.movingCamera();
  }
}
